package com.camunda.workflow.service;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
public class SelfRepairService {
    /**
     * topicName ： 订阅的主题
     * processDefinitionKeyIn ： 流程ID
     * lockDuration ： 外部任务超时时间
     */
    @Bean
    @ExternalTaskSubscription(topicName = "try_self_repair", processDefinitionKeyIn = {"External_Task_Repair"}, lockDuration = 5)
    public ExternalTaskHandler doSelfRepair() {
        return (externalTask, externalTaskService) -> {
            log.info("外部任务进入偿试自修");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            boolean isFree = externalTask.getVariable("isFree");
            if (isFree) {
                log.info("免费维修");
                // 后面两个参数分别是 ： 重试次数，重试时间间隔
                externalTaskService.handleFailure(externalTask, "维修是免费的，我不想自修了", "这里可以打印异常stacktrace", 1, 5000);
            } else {
                log.info("收费维修");
                externalTaskService.complete(externalTask);
            }
        };
    }

    @Bean
    @ExternalTaskSubscription(topicName = "checkNegtive", processDefinitionKeyIn = {"Gateway_Parallel_Upload_Video","Subprocess_Embedded","Subprocess_Call_Sub","Execution_Activity_Diff"}, lockDuration = 5000000)
    public ExternalTaskHandler checkVideoNegtive() {
        return (externalTask, externalTaskService) -> {
            log.info("进入检查视频是否负面");
            Object videoName = externalTask.getVariable("videoName");
            log.info("视频名称：" + videoName + "不包含负面内容");
            externalTaskService.complete(externalTask);
        };
    }

    @Bean
    @ExternalTaskSubscription(topicName = "wechatPay", processDefinitionKeyIn = {"Event_message_Order_Food","Pool_Lane"}, lockDuration = 50000)
    public ExternalTaskHandler wechatPay() {
        return (externalTask, externalTaskService) -> {
            log.info("微信支付");
            externalTaskService.complete(externalTask);

        };
    }


    @Bean
    @ExternalTaskSubscription(topicName = "alipay", processDefinitionKeyIn = {"Event_message_Order_Food","Pool_Lane"}, lockDuration = 50000)
    public ExternalTaskHandler alipay() {
        return (externalTask, externalTaskService) -> {
            log.info("阿里支付");
            externalTaskService.complete(externalTask);

        };
    }

    @Bean
    @ExternalTaskSubscription(topicName = "success_pay", processDefinitionKeyIn = {"Event_message_Order_Food","Pool_Lane"}, lockDuration = 50000)
    public ExternalTaskHandler successPay() {
        return (externalTask, externalTaskService) -> {
            log.info("支付成功");
            externalTaskService.complete(externalTask);

        };
    }


}

